package com.woniuxy.lrz.carlproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.Log;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.controller.form.levelform.UpdateFirstLevelForm;
import com.woniuxy.lrz.carlproject.model.ServiceFirstLevel;
import com.woniuxy.lrz.carlproject.model.ServiceSecondLevel;
import com.woniuxy.lrz.carlproject.service.ServiceFirstLevelService;
import com.woniuxy.lrz.carlproject.service.ServiceSecondLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 一级分类服务表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/carlproject/serviceFirstLevel")
@Api("一级服务接口")
@Slf4j
public class ServiceFirstLevelController {

    @Resource
    private ServiceFirstLevelService serviceFirstLevelService;
    @Resource
    private ServiceSecondLevelService serviceSecondLevelService;

    @GetMapping("/getFirstLevel")
    @Log("查询一级服务")
    @ApiOperation("查询一级服务")
    public Result getFirstLevel(int current,int size){
        Page<ServiceFirstLevel> all = serviceFirstLevelService.getAll(current, size);
        return Result.success(all);
    }

    @GetMapping("/addFirstLevel")
    @Log("添加一级服务")
    @ApiOperation("添加一级服务")
    @ApiImplicitParam("服务名")
    public Result addFirstLevel(String name){
        log.info("一级服务名:{}",name);
        if (name==null || name.equals("")){
            throw  new AppException("参数错误");
        }
        QueryWrapper<ServiceFirstLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("service_first_name",name);
        ServiceFirstLevel one = serviceFirstLevelService.getOne(wrapper);
        if (one!=null){
            throw  new AppException("该服务名已经存在");
        }
        ServiceFirstLevel level = new ServiceFirstLevel();
        level.setServiceFirstName(name);
        level.setDeleted(0);
        boolean save = serviceFirstLevelService.save(level);
        if (!save){
            throw  new AppException("添加失败");
        }
        return Result.success();
    }

  @PutMapping("/updateFirstLevel")
  @Log("修改一级服务")
  @ApiOperation("修改一级服务")
  @ApiImplicitParam("修改一级服务表单")
   public  Result updateFirstLevel(@RequestBody @Valid UpdateFirstLevelForm updateFirstLevelForm){
        log.info("一级服务表单:{}",updateFirstLevelForm);
      QueryWrapper<ServiceFirstLevel> wrapper1 = new QueryWrapper<>();
      wrapper1.eq("service_first_name",updateFirstLevelForm.getServiceFirstName());
      ServiceFirstLevel one = serviceFirstLevelService.getOne(wrapper1);
      if (one!=null){
          throw  new AppException("该服务名已经存在");
      }
      UpdateWrapper<ServiceFirstLevel> wrapper = new UpdateWrapper<>();
        wrapper.eq("service_first_id",updateFirstLevelForm.getServiceFirstId())
                .set("service_first_name",  updateFirstLevelForm.getServiceFirstName());
      boolean update = serviceFirstLevelService.update(wrapper);
      if (!update){
          throw  new AppException("修改一级服务失败");
      }
      return  Result.success();
  }


  @DeleteMapping("/delete")
  @Log("删除一级服务(逻辑删除)")
  @ApiOperation("删除一级服务(逻辑删除)")
  @ApiImplicitParam("服务id")
  public  Result deleteByFirstId(Integer id){
        log.info("一级服务id:{}",id);
        if (id<1 || id==null) {
            throw new AppException("参数错误");
        }
        //拿一级服务id查二级如果有则不允许删除
      QueryWrapper<ServiceSecondLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("service_first_id",id);
      List<ServiceSecondLevel> list = serviceSecondLevelService.list(wrapper);
      if (list.size()>0){
          throw  new AppException("该类下有关联服务删除失败");
      }else {
          UpdateWrapper<ServiceFirstLevel> wrapper1 = new UpdateWrapper<>();
          wrapper1.eq("service_first_id",id)
                  .set("deleted",1);
          boolean update = serviceFirstLevelService.update(wrapper1);
          if (!update){
              throw  new AppException("逻辑删除一级服务失败");
          }
      }
      return Result.success();
  }
}
